<?php
$loader->requireOnce('includes/Icd10Selector/Icd10Selector.class.php');

class C_EyeExamination extends Controller {
	
	private $icd10Selector;
	
	const ENABLE_SECONDARY_ICD = true;

	public function C_EyeExamination($template_mod = "general") {
		parent::Controller();		
		$this->template_mod = $template_mod;
		$this->view->path 	= 'eye_examination';
		
		$this->icd10Selector = new Icd10Selector();
	}	

	public function actionLoadEyeExamination(){
		$recordId	=	$_GET['recordId'];
		$encounter	=	ORDataObject::factory('Encounter')->getObjectFromRecordId($recordId);
		$commonInfo = 	ORDataObject::factory('EncounterCommonInfo')->getObjectFromRecordId($recordId);
		if($encounter->getId() > 0){
			$doctorName	=	$encounter->getDoctorName();
			$doctorId	=	$encounter->getDoctorId();
			$doctorUpdate = $encounter->getUpdatedDoctorName();
			$created = L10nHelper::renderDateTime($encounter->get('encounter_date'), 'medium', 'datetime');
			$updated = L10nHelper::renderDateTime($encounter->get('last_updated'), 'medium', 'datetime');
		} else {
			$person		= ORDataObject::factory("Person");
			$person->setup($this->_me->get_person_id());
			$doctorName	=	$person->getName();
			$doctorId	=	$person->getId();
			$doctorUpdate = $doctorName;
			$created = L10nHelper::renderDateTime(date('Y-m-d H:m:s'), 'medium', 'datetime');
			$updated = L10nHelper::renderDateTime(date('Y-m-d H:m:s'), 'medium', 'datetime');
		}

		$icd10		=	ORDataObject::factory('Icd10');
		$icd10->setup($encounter->getPrimaryIcd10());
		
        $this->icd10Selector->initIcd10ForController($this, Icd10Selector::EYE_CHAPTER); 
        
		$patientId = Celini::newORDO('MedicalRecord', $recordId)->get('patient_id');

		$this->assign_by_ref('patient', Celini::newORDO('Patient', $patientId));
		$this->assign_by_ref('priIcd10', $icd10);
		$this->assign_by_ref('encounter', $encounter);
		$this->assign_by_ref('commonInfo', $commonInfo);
		$this->assign('doctorName', $doctorName);
		$this->assign('doctorId', $doctorId);
		$this->assign('doctorUpdate', $doctorUpdate);
		$this->assign('createdDate', $created);
		$this->assign('updatedDate', $updated);
		$this->assign('recordId', $recordId);

		if (self::ENABLE_SECONDARY_ICD) {
			$icd10_1	=	ORDataObject::factory('Icd10');
			$icd10_1->setup($encounter->getSecondaryIcd10_1());
			$icd10_2	=	ORDataObject::factory('Icd10');
			$icd10_2->setup($encounter->getSecondaryIcd10_2());
			$this->assign_by_ref('sec1Icd10', $icd10_1);
			$this->assign_by_ref('sec2Icd10', $icd10_2);
		}
		$this->assign('enableSecondary', self::ENABLE_SECONDARY_ICD);

		return $this->view->render('edit_eye_examination.html');
	}

	public function actionEditEyeExamination(){
		$primIcd10Code	=	Utils::sanitizeText($_POST['clinDiagPrimaryIcd10']);
		if($primIcd10Code!='' && !$this->icd10Selector->validateIcd10Code($primIcd10Code)){
			$response['saved']			=	0;
			$response['errorField']		=	"clinDiagPrimaryIcd10";
			$response['errorMessage']	=	L10nHelper::_("Not exist");
			return json_encode($response);
		}

		$_POST['clinDiagNote'] 		= 	Utils::sanitizeText($_POST['clinDiagNote'], false);
		$_POST['clinDiagSymptom'] 	= 	Utils::sanitizeText($_POST['clinDiagSymptom'], false);
		$_POST['clinDiagLeftBPT'] 	= 	Utils::sanitizeText($_POST['clinDiagLeftBPT']);
		$_POST['clinDiagLeftBPS'] 	= 	Utils::sanitizeText($_POST['clinDiagLeftBPS']);
		$_POST['clinDiagRightBPT'] 	= 	Utils::sanitizeText($_POST['clinDiagRightBPT']);
		$_POST['clinDiagRightBPS'] 	= 	Utils::sanitizeText($_POST['clinDiagRightBPS']);
		$_POST['chandoan'] 			= 	Utils::sanitizeText($_POST['chandoan'], false);

		$enId	=(int)	$_POST['encounterId'];
		$encounter	=	&ORDataObject::factory('Encounter');
		$encounterDate = '';
		if($enId != 0){
			$encounter->setId($enId);
			$encounterDate = 'already';
		}
		if($_POST['doctorId']!='') {
			$doctor_id	=	$_POST['doctorId'];
		} else {
			$doctor_id	=	$this->_me->get_person_id();
		}
		$doctor_update_id = $this->_me->get_person_id();
		$icd10Id	=	ORDataObject::factory('Icd10')->getObjectbyCode($primIcd10Code)->getId();

		if (self::ENABLE_SECONDARY_ICD) {
			$secIcd10Code_1	=	Utils::sanitizeText($_POST['clinDiagSecondaryIcd10_1']);
			if ($secIcd10Code_1!='' && !$this->icd10Selector->validateIcd10Code($secIcd10Code_1)) {				 				
				$response['saved']			=	0;
				$response['errorField']		=	"clinDiagSecondaryIcd10_1";
				$response['errorMessage']	=	L10nHelper::_("Not exist");
				return json_encode($response);
			}
			$secIcd10Code_2	=	Utils::sanitizeText($_POST['clinDiagSecondaryIcd10_2']);
			if($secIcd10Code_2!='' && !$this->icd10Selector->validateIcd10Code($secIcd10Code_2)){
				$response['saved']			=	0;
				$response['errorField']		=	"clinDiagSecondaryIcd10_2";
				$response['errorMessage']	=	L10nHelper::_("Not exist");
				return json_encode($response);
			}
			$icd10Id_1	=	ORDataObject::factory('Icd10')->getObjectbyCode($secIcd10Code_1)->getId();
			$icd10Id_2	=	ORDataObject::factory('Icd10')->getObjectbyCode($secIcd10Code_2)->getId();
		} else {
			$icd10Id_1 = '';
			$icd10Id_2 = '';
		}

		$encounter->setData($_POST['recordId'], $_POST['clinDiagSymptom'],$_POST['clinDiagLeftTension'],
			$_POST['clinDiagRightTension'],$_POST['clinDiagLeftBPT'],$_POST['clinDiagLeftBPS'],
			$_POST['clinDiagRightBPT'],$_POST['clinDiagRightBPS'], $_POST['chandoan'], $icd10Id, $icd10Id_1,
			$icd10Id_2, $_POST['clinDiagTreatment'],$_POST['clinDiagNote'], $doctor_id);
		$response['encounterId']=$encounter->getId();
		$response['encounterSymptom']=$encounter->getSymptom();
		$commonInfo = 	ORDataObject::factory('EncounterCommonInfo');
		$commonInfoId = $commonInfo->getIdFromRecordId($_POST['recordId']);
		if ($commonInfoId > 0) {
			$commonInfo->setup($commonInfoId);
		}
		$commonInfo->setCvData($_POST['recordId'], $_POST['leftCVA'], $_POST['leftUCVA'], $_POST['rightCVA'],
			$_POST['rightUCVA'], $_POST['bothEyesight']);

		$patientId = Celini::newORDO('MedicalRecord', $_POST['recordId'])->get('patient_id');
		$p = Celini::newORDO('Patient', $patientId);
		if (isset($_POST['potentialPhaco']) && $_POST['potentialPhaco'] == 'on') {
			$p->set('potential_for', 'phaco');
		} else {
			$p->set('potential_for', '');
		}
		$p->persist();

		if($encounter->persist($encounterDate)){
			$commonInfo->persist();
			$response['saved'] = 1;
			$response['encId'] = $encounter->get('id');
		} else {
			$response['saved'] = 0;
		}
		return json_encode($response);
	}
}
?>